local module = {}
module.name = "Cooldown Timer"
module.Init = function()
	if not SCDB.modules[module.name] then return end

	function gft(s)
		if (s < 3600 and s >= 60) then
			return format("%dm", floor(s / 60 + 1))
		elseif (s < 1) then
			return format("%.1f", s)
		elseif (s < 60) then
			return floor(s+1)
		else
			return format("%dh", floor(s/ 3600 + 1))
		end
	end

	local function UpdateTimer(self, elapsed)
		local remaining = self.endTime - GetTime()
		if remaining > 0 and self.text:IsShown() then
			local font,_,fstyle = self.text:GetFont()
			self.text:SetFont(font, 0.4*self:GetHeight()+1, fstyle)
			self.text:SetText(gft(remaining))

			if remaining > 6 then
				self.text:SetTextColor(1,1,1)
			else
				self.text:SetTextColor(1,0,0)
			end
		else
			self.text:SetText("")
			self.text:Hide()
			self:SetScript("OnUpdate", nil)
		end
	end

	hooksecurefunc(getmetatable(ActionButton1Cooldown).__index, "SetCooldown", function(self, start, duration, test)
		if start > 0 and duration > 3 then
			self.endTime = start + duration
			self.text = self.text or self:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
			self.text:SetPoint("BOTTOM", 0, 2)
			self:SetScript("OnUpdate", UpdateTimer)
			self.text:Show()
		elseif self.text then
    		self.text:Hide()
			self:SetScript("OnUpdate", nil)
		end
	end)

end
tinsert(SuperClassic.modules, module) -- finish him!